package cn.a7e7.idanalysis;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = "DatabaseHelper";

    /**
     * @ context 上下文
     * @ name    数据库名称
     * @ factory 游标工厂
     * @ version 版本号
     */
    public DatabaseHelper(@Nullable Context context) {
        super(context, Constants.DATABASE_NAME, null, Constants.VERSION_CODE);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建时的回调
        Log.d(TAG, "创建数据库。。。");

        // 创建数据表
        String sql = "CREATE TABLE " + Constants.DATABASE_TABLE_NAME +
                "(id INT PRIMARY KEY NOT NULL," +
                "AreaCode int NOT NULL," +
                "ProvinceCode INT NOT NULL," +
                "LandCode int NOT NULL," +
                "CountyCode int NOT NULL," +
                "UnitName char(50) NOT NULL" +
                ");";
        db.execSQL(sql);
        Log.d(TAG, "创建数据表。。。");

        // 导入数据
        String[] strArr = Constants.SQL_DATA_ARR;
        for (int i = 0; i < Constants.SQL_DATA_ARR.length; i++) {
            db.execSQL("INSERT INTO " + Constants.DATABASE_TABLE_NAME + " VALUES(" + strArr[i] + ");");
        }
        Log.d(TAG, "插入数据。。。");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //升级数据库时的回调
        Log.d(TAG, "升级数据库。。。");
    }

}
